package com.hmdp.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.hmdp.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private Client client;

    @Override
    public void sendCode(String phone, String code) {
        //创建短信发送请求对象：
        SendSmsRequest smsRequest = new SendSmsRequest();        //设置手机号码：
        smsRequest.setPhoneNumbers(phone);
        //设置签名名称：
        smsRequest.setSignName("阿里云短信测试");
        //设置模板代码：
        //
        smsRequest.setTemplateCode("SMS_154950909");
        //设置模板参数：

        smsRequest.setTemplateParam("{\"code\":\"" + code + "\"}");
        try {
            client.sendSms(smsRequest);//发送短信
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}